#include "MainWidget.h"
#include "ui_MainWidget.h"
#include "WidgetHandler.h"
#include "BootSetting.h"
#include "ImageSetting.h"
#include "MiscData.h"
#include "AppLog.h"
#include "McuWorker.h"
#include "ScreenshotWorker.h"
#include "QmlVirtualKeyboard.h"
#ifdef arm
#include "Mpp.h"
#include "Mcu.h"
#endif

#define VIDEO_WIDTH          (1280)
#define VIDEO_HEIGHT         (800)


MainWidget::MainWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWidget)
{
    ui->setupUi(this);
    ui->image->installEventFilter(this);

    iLog(ARM_VERSION);

    loadSystemConfig();
    kbd = new QmlVirtualKeyboard(this);
    WidgetHandler::getInstance()->init(ui->widget);
    ScreenshotWorker::getInstance()->init((QObject*)this);
    if (RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.isFirstTimeLogin == 0) {/* 首次使用 */
        WidgetHandler::getInstance()->displayWidget(BootUi_Id);
//        WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
    } else {/* 非首次使用 */
        if (RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.loginMode == 0) {/* 需要登录 */
            WidgetHandler::getInstance()->displayWidget(LoginUi_Id);
        } else {/* 直接进入主界面 */
            WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
        }
    }

#ifdef arm
    /* init mpp */
    qDebug()<<"mpp version:"<<Mpp::getInstance()->readMppVersion();
    QRect rect(0, 0, VIDEO_WIDTH, VIDEO_HEIGHT);
    Mpp::getInstance()->initCamera(rect);
    Mpp::getInstance()->startRealtimeImage();
    /* init mcu */
    Mcu::getInstance()->init();
    QObject::connect(Mcu::getInstance(), &Mcu::reportKeyValueSignal, McuWorker::getInstance(), &McuWorker::reportKeyValueSlot);
    int touchSound = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.touchSound;
//    if (touchSound < 0)/* 静音 */
        Mcu::getInstance()->setTouchSoundBeepStateMessage(false);
#endif

    applySystemConfig();
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;

    default:
        break;
    }
}

bool MainWidget::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->image && event->type() == QEvent::MouseButtonPress) {
        Mcu::getInstance()->touchSoundBeepMessage();

        if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.fullScreenEntryType == 0) {
            WidgetHandler::getInstance()->displayWidget(HomeUi_Id);
        } else if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.fullScreenEntryType == 1) {
            WidgetHandler::getInstance()->displayWidget(ImageSettingUi_Id);
        } else if (RuntimeData::MiscData::getInstance()->g_miscDataInfo.fullScreenEntryType == 2) {
            WidgetHandler::getInstance()->displayWidget(VideoPlayerUi_Id);
        }
    }
}

void MainWidget::loadSystemConfig() {
    RuntimeData::BootSetting::getInstance()->loadBootSettingInfo();
    RuntimeData::ImageSetting::getInstance()->loadImageSettingInfo();
}

extern void (*setFpga[])( uint32_t value );
void MainWidget::applySystemConfig() {
//    int mirrorType = RuntimeData::BootSetting::getInstance()->g_bootSettingInfo.currentMirrorType;
//    for (int i = 0; i < IMAGE_SETTING_ITEM_COUNT; i++) {
//        if(i!=10)setFpga[i](RuntimeData::ImageSetting::getInstance()->g_settingItemValue[mirrorType][i]);
//    }
}

void MainWidget::mousePressEvent(QMouseEvent */*event*/) {
    kbd->hide();
}
